package com.mind.adjust.service.impl;

import com.mind.common.exception.ServiceException;
import com.mind.customer.mapper.AjBasicGoodsitemMapper;
import com.mind.system.domain.AjBasicGoodsitem;
import com.mind.adjust.service.AjBasicGoodsitemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * (AjBasicGoodsitem)表服务实现类
 *
 * @author makejava
 * @since 2024-10-14 17:40:43
 */
@Service("basicGoodsitemService")
public class AjBasicGoodsitemServiceImpl implements AjBasicGoodsitemService {
    @Autowired
    private AjBasicGoodsitemMapper basicGoodsitemMapper;

    /**
     * 查询信息集合
     *
     * @param basicGoodsitem 信息
     * @return 信息集合
     */
    @Override
    public List<AjBasicGoodsitem> selectBasicGoodsitemList(AjBasicGoodsitem basicGoodsitem) {
        return basicGoodsitemMapper.selectBasicGoodsitemList(basicGoodsitem);
    }

    /**
     * 通过ID查询信息
     *
     * @param basicGoodsitemId ID
     * @return 角色对象信息
     */
    @Override
    public AjBasicGoodsitem selectBasicGoodsitemById(Long basicGoodsitemId) {
        return basicGoodsitemMapper.selectBasicGoodsitemById(basicGoodsitemId);
    }

    /**
     * 删除信息
     *
     * @param basicGoodsitemId ID
     * @return 结果
     */
    @Override
    public int deleteBasicGoodsitemById(Long basicGoodsitemId) {
        return basicGoodsitemMapper.deleteBasicGoodsitemById(basicGoodsitemId);
    }

    /**
     * 批量删除信息
     *
     * @param basicGoodsitemIds 需要删除的ID
     * @return 结果
     */
    @Override
    public int deleteBasicGoodsitemByIds(Long[] basicGoodsitemIds) {
        return basicGoodsitemMapper.deleteBasicGoodsitemByIds(basicGoodsitemIds);
    }

    /**
     * 新增保存信息
     *
     * @param basicGoodsitem 信息
     * @return 结果
     */
    @Override
    public int insertBasicGoodsitem(AjBasicGoodsitem basicGoodsitem) {
        return basicGoodsitemMapper.insertBasicGoodsitem(basicGoodsitem);
    }

    /**
     * 修改保存信息
     *
     * @param basicGoodsitem 信息
     * @return 结果
     */
    @Override
    public int updateBasicGoodsitem(AjBasicGoodsitem basicGoodsitem) {
        return basicGoodsitemMapper.updateBasicGoodsitem(basicGoodsitem);
    }

    /**
     * 批量插入
     *
     * @param basicGoodsitemList 附件类型信息
     * @return 结果
     */
    @Override
    public int batchInsert(List<AjBasicGoodsitem> basicGoodsitemList) {
        int size = 0;
        for (AjBasicGoodsitem basicGoodsitem : basicGoodsitemList) {
//            if (basicGoodsitemMapper.selectBasicGoodsitemByName(basicGoodsitem.getName()) == null){
//                basicGoodsitemMapper.insertBasicGoodsitem(basicGoodsitem);
//                size ++;
//            }
        }
        return size;
    }

    @Override
    public int copy(Map<String, Object> params) {
        if (!params.containsKey("ids")) {
            throw new ServiceException("缺少ids参数");
        }
        String ids = (String) params.get("ids");
        String[] idArr = ids.split(",");
        List<AjBasicGoodsitem> basicGoodsitemList = new ArrayList<>();
        for (String id : idArr) {
            AjBasicGoodsitem basicGoodsitem = basicGoodsitemMapper.selectBasicGoodsitemById(Long.parseLong(id));
            if (basicGoodsitem == null) {
                throw new ServiceException("id为" + id + "的记录不存在");
            }
            basicGoodsitemList.add(basicGoodsitem);
        }
        if (basicGoodsitemList.size() > 0) {
            for (AjBasicGoodsitem basicGoodsitem : basicGoodsitemList) {
                basicGoodsitem.set编码(null);
                basicGoodsitem.set商品名称(basicGoodsitem.get商品名称() + "_copy");
                basicGoodsitemMapper.insertBasicGoodsitem(basicGoodsitem);
            }
            return basicGoodsitemList.size();
        }
        return 0;
    }
}
